package com.cnfangmao.masterdata.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnfangmao.masterdata.dao.AccountBasicDataRightDao;
import com.cnfangmao.masterdata.dto.AccountBasicDataRightSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.AccountBasicDataRight;
import com.cnfangmao.masterdata.model.BasicDataMetadata;
import com.cnfangmao.masterdata.service.AccountBasicDataRightService;

/**
 * <p>
 * 用户主数据权限表 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Service
public class AccountBasicDataRightServiceImpl extends ServiceImpl<AccountBasicDataRightDao, AccountBasicDataRight> implements AccountBasicDataRightService {

	@Autowired
	private AccountBasicDataRightDao accountBasicDataRightDao;
	
	/**    
	 * @description: 根据账号Id，获取有权限的主数据列表  
	 * @author: chenxc
	 * @date: 2020年4月20日 下午4:33:15
	 * @param userId
	 * @return   
	 * @see com.cnfangmao.masterdata.service.AccountBasicDataRightService#getMasterdataByUserId(java.lang.String)   
	 */
	@Override
	public List<BasicDataMetadata> getMasterdataByUserId(String userId) {
		return this.accountBasicDataRightDao.getMasterdataByUserId(userId);
	}

	@Override
	public AccountBasicDataRight findByUserIdAndMetadataId(String userId,
			String metadataId) {
		AccountBasicDataRight accountBasicDataRight =null;
		QueryWrapper<AccountBasicDataRight> queryWrapper =new QueryWrapper<AccountBasicDataRight>();
		queryWrapper.eq("user_id", userId);
		queryWrapper.eq("metadata_id", metadataId);
		accountBasicDataRight=accountBasicDataRightDao.selectOne(queryWrapper);
		return accountBasicDataRight;
	}

	@Override
	public IPage<AccountBasicDataRight> queryListAccountBasicDataRight(
			GeneralSearchDto<AccountBasicDataRightSearchDto>  dto) {
		IPage<AccountBasicDataRight> page = new Page<AccountBasicDataRight>(dto.getPageNo(), dto.getPageSize());
		IPage<AccountBasicDataRight> listPage=accountBasicDataRightDao.queryListAccountBasicDataRight(page,dto.getConditions().getUserCode(), dto.getConditions().getMetadataCode());
		return listPage;
	}

	@Override
	public boolean checkPermissionByAccountBasicDataRight(String userId,
			String metadataId, Integer rightList) {
		boolean result =false;
		AccountBasicDataRight accountBasicDataRight =null;
		QueryWrapper<AccountBasicDataRight> queryWrapper =new QueryWrapper<AccountBasicDataRight>();
		queryWrapper.eq("user_id", userId);
		queryWrapper.eq("metadata_id", metadataId);
		accountBasicDataRight=accountBasicDataRightDao.selectOne(queryWrapper);
		if(accountBasicDataRight!=null)
		{
			result = (rightList & accountBasicDataRight.getRightList())>0;
		}
		return result;
	}

}
